from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.select import Select
import time
import random


def get_sel(web, xpath):
    sel_elm = web.find_element(by='xpath', value=xpath)
    sel = Select(sel_elm)
    return sel


# 参数配置 设置无头浏览器
opt = Options()
opt.add_argument('--headless')
opt.add_argument('--disable-gpu')


chrome = Chrome(options=opt)
chrome.get('https://www.dajiazhao.com/sm/')
print(chrome.title)

chrome.find_element(by='xpath', value='//*[@id="left"]/div[1]/dl/dd/form/p[1]/input[1]').send_keys('张')
chrome.find_element(by='xpath', value='//*[@id="left"]/div[1]/dl/dd/form/p[1]/input[2]').send_keys('三')

sex_sel = get_sel(chrome, '//*[@id="left"]/div[1]/dl/dd/form/p[1]/select')
year_sel = get_sel(chrome, '//*[@id="left"]/div[1]/dl/dd/form/p[2]/select[1]')
mon_sel = get_sel(chrome, '//*[@id="left"]/div[1]/dl/dd/form/p[2]/select[2]')
day_sel = get_sel(chrome, '//*[@id="left"]/div[1]/dl/dd/form/p[2]/select[3]')

sex_sel.select_by_index(1)
year_sel.select_by_index(random.randint(0, len(year_sel.options)))
mon_sel.select_by_index(random.randint(0, len(mon_sel.options)))
day_sel.select_by_index(random.randint(0, len(day_sel.options)))

chrome.find_element(by='xpath', value='//*[@id="left"]/div[1]/dl/dd/form/p[2]/input').click()
time.sleep(1)

for i in range(1, 12):
    print(chrome.find_element(by='xpath', value=f'//*[@id="left"]/table[{i}]').text)

# # 获取页面源代码
# print(chrome.page_source)

time.sleep(100)
